﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.EmpbasicService;
using EHR.Common;
using org.in2bits.MyXls;

namespace CommunitySystem.PageView
{
    public class ShiTi1
    {
        public string ygzt { get; set; }
    }
    public class ShiTi2
    {
        public string djbd { get; set; }
    }
    //如果绑定combobox，并且希望数据源时时变动，就用这种定义类的形式，并且在开始就绑定到combobox上
    public class ComObservable
    {
        private string _name;
        public string name
        {
            get { return _name; }
            set { _name = value; }
        }
    }
    public partial class CheGaiControl : UserControl
    {
        public String[] Oldorgids;
        public ObservableCollection<EmpbasicService.OrgForHRModel> OrgList =
            new ObservableCollection<EmpbasicService.OrgForHRModel>();
        public ObservableCollection<EmpbasicService.OrgForHRModel> forumList =
            new ObservableCollection<EmpbasicService.OrgForHRModel>();
        public ObservableCollection<EmpbasicService.CheGaiModel> CheGaiModelList =
            new ObservableCollection<EmpbasicService.CheGaiModel>();
        public ObservableCollection<EmpbasicService.CheGaiModel> selectedempmodel =
            new ObservableCollection<EmpbasicService.CheGaiModel>();
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        private PublicOfficialSelectionService.PublicOfficialSelectionServiceClient Client1 =
           new PublicOfficialSelectionService.PublicOfficialSelectionServiceClient();
        private ShiTi1 shiti = new ShiTi1();
        private ShiTi2 shiti2 = new ShiTi2();
        private ObservableCollection<ComObservable> tem = new ObservableCollection<ComObservable>();
        public CheGaiControl()
        {
            InitializeComponent();
            shiti.ygzt = "未导出";
            ygzt.DataContext = shiti;
            shiti2.djbd = "开卡表";
            djbd.DataContext = shiti2;
            tem.Add(new ComObservable { name = "无" });
            biaoti.ItemsSource = tem;
            djbd.OnSelectionChanged += new OnSelectionChangedEvent(djbd_OnSelectionChanged);
            //Client.UpdateCheGaiAllCompleted += new EventHandler<UpdateCheGaiAllCompletedEventArgs>(Client_UpdateCheGaiAllCompleted);
            Client.GetDengLuJueSeCompleted += new EventHandler<EmpbasicService.GetDengLuJueSeCompletedEventArgs>(Client_GetDengLuJueSeCompleted);
            Client.GetDengLuJueSeAsync(Session.LoginUser.LoginName);
            Client.GetCheGaiCountsCompleted += new EventHandler<EmpbasicService.GetCheGaiCountsCompletedEventArgs>(Client_GetCheGaiCountsCompleted);
            Client.GetCheGaiCompleted += new EventHandler<EmpbasicService.GetCheGaiCompletedEventArgs>(Client_GetCheGaiCompleted);
            Client.GetOrgForHRModelCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelCompletedEventArgs>(Client_GetOrgForHRModelCompleted);
            Client.GetDengLuOrgCompleted += new EventHandler<EmpbasicService.GetDengLuOrgCompletedEventArgs>(Client_GetDengLuOrgCompleted);
            Client.UpdateCheGaiCompleted += new EventHandler<UpdateCheGaiCompletedEventArgs>(Client_UpdateCheGaiCompleted);
            Client.GetKeyIDsCompleted += new EventHandler<GetKeyIDsCompletedEventArgs>(Client_GetKeyIDsCompleted);
            Client.GetOrgForHRModelbyIDsCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelbyIDsCompletedEventArgs>(Client_GetOrgForHRModelbyIDsCompleted);
            Client.DeteleCheGaiCompleted += new EventHandler<DeteleCheGaiCompletedEventArgs>(Client_DeteleCheGaiCompleted);
        }

        

        void Client_UpdateCheGaiAllCompleted(object sender, UpdateCheGaiAllCompletedEventArgs e)
        {

        }

        void djbd_OnSelectionChanged(object sender, object NewValue, object OldValue)
        {
            if (NewValue != null)
            {
                if (NewValue.ToString() == "开卡表")
                {
                    DGRole.Columns[6].Visibility = System.Windows.Visibility.Visible;
                    DGRole.Columns[7].Visibility = System.Windows.Visibility.Visible;
                    DGRole.Columns[10].Header = "备注";
                    DGRole.Columns[3].Visibility = System.Windows.Visibility.Collapsed;
                    DGRole.Columns[4].Visibility = System.Windows.Visibility.Collapsed;
                    DGRole.Columns[5].Visibility = System.Windows.Visibility.Collapsed;
                }
                if (NewValue.ToString() == "变动表")
                {
                    DGRole.Columns[6].Visibility = System.Windows.Visibility.Collapsed;
                    DGRole.Columns[7].Visibility = System.Windows.Visibility.Collapsed;
                    DGRole.Columns[10].Header = "变动理由";
                    DGRole.Columns[3].Visibility = System.Windows.Visibility.Visible;
                    DGRole.Columns[4].Visibility = System.Windows.Visibility.Visible;
                    DGRole.Columns[5].Visibility = System.Windows.Visibility.Visible;
                }
                selectedempmodel.Clear();
                Load();
            }

        }



        void Client_UpdateCheGaiCompleted(object sender, UpdateCheGaiCompletedEventArgs e)
        {

        }

        void Client_GetOrgForHRModelCompleted(object sender, EmpbasicService.GetOrgForHRModelCompletedEventArgs e)
        {
            OrgList = e.Result;
            forumList.Clear();
            foreach (EmpbasicService.OrgForHRModel i in e.Result)
            {
                forumList.Add(i);
            }
            AddTreeNode("全部单位", null);
        }

        private void AddTreeNode(string type, TreeViewItem treeViewItem)
        {
            List<EmpbasicService.OrgForHRModel> result = (from forumInfo in forumList
                                                          where forumInfo.Type == type
                                                          select forumInfo).ToList<EmpbasicService.OrgForHRModel>();

            if (result.Count > 0)
            {
                foreach (EmpbasicService.OrgForHRModel foruminfo in result)
                {
                    TreeViewItem objTreeNode = new TreeViewItem();

                    //StackPanel sp = new StackPanel();
                    CheckBox cb = new CheckBox();
                    cb.Content = foruminfo.Name;
                    cb.Tag = objTreeNode;
                    cb.DataContext = foruminfo;
                    //sp.Children.Add(cb);
                    cb.Click += new RoutedEventHandler(cb_Checked);
                    objTreeNode.Header = cb;
                    //objTreeNode.Header = foruminfo.Name;
                    //objTreeNode.DataContext = foruminfo;
                    //objTreeNode.Tag = foruminfo.ID;
                    if ((objTreeNode.Header as CheckBox).Content.ToString() == "组织机构") objTreeNode.IsExpanded = true;

                    //此样式将会添加的所有叶子结点上
                    //objTreeNode.ItemContainerStyle = this.Resources["RedItemStyle"] as Style;

                    //添加根节点
                    if (treeViewItem == null)
                    {
                        TreeOfLife.Items.Add(objTreeNode);
                    }
                    else
                    {
                        treeViewItem.Items.Add(objTreeNode);
                    }
                    AddTreeNode(foruminfo.Name, objTreeNode);
                }
            }
        }

        private int OrgID = 0;
        public List<string> orgids = new List<string>();
        void cb_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            EmpbasicService.OrgForHRModel rm = cb.DataContext as EmpbasicService.OrgForHRModel;
            if (cb.IsChecked == true)
            {
                if (!orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                    orgids.Add(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());


                CheckChildItem(cb.Tag as TreeViewItem);
                string ss = "";
                foreach (string i in orgids)
                    ss += i + ",";
                if (string.IsNullOrEmpty(ss) || orgids.Contains("79")) ss = "0";
                else ss = ss.Substring(0, ss.Length - 1);
                Client.GetKeyIDsAsync(datepicker.SelectedDate.ToString(), datepicker1.SelectedDate.ToString(), ss);
            }
            else
            {
                //if (orgids != null)
                //{
                if (orgids != null)
                {
                    if (orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Remove(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                }

                UnCheckChildItem(cb.Tag as TreeViewItem);
                string ss = "";
                foreach (string i in orgids)
                    ss += i + ",";
                if (string.IsNullOrEmpty(ss) || orgids.Contains("79")) ss = "0";
                else ss = ss.Substring(0, ss.Length - 1);
                Client.GetKeyIDsAsync(datepicker.SelectedDate.ToString(), datepicker1.SelectedDate.ToString(), ss);
            }

        }

        void CheckChildItem(TreeViewItem item)
        {
            foreach (TreeViewItem tvi in item.Items)
            {
                if (tvi != null)
                {
                    CheckBox cb = tvi.Header as CheckBox;
                    cb.IsChecked = true;
                    if (!orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Add(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                    CheckChildItem(tvi);
                }
            }
        }
        void UnCheckChildItem(TreeViewItem item)
        {
            foreach (TreeViewItem tvi in item.Items)
            {
                if (tvi != null)
                {
                    CheckBox cb = tvi.Header as CheckBox;
                    cb.IsChecked = false;
                    if (orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Remove(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                    UnCheckChildItem(tvi);
                }
            }
        }
        public int orgid { get; set; }
        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            orgid = Convert.ToInt32(((OrgForHRModel)(ListBoxOfLife.SelectedItem)).ID);
            orgids.Clear();
            orgids.Add(orgid.ToString());
            Load();
        }

        private GridLength _lastColLeftLength;
        private GridLength _lastColRightLength;
        private void GridSpliteTo_ToLeft(object sender, EventArgs e)
        {
            //已经到最左边，直接返回
            if (colLeft.Width.Value == 0.0)
            {
                return;
            }

            //
            if (colRight.Width.Value == 0)
            {
                colLeft.Width = _lastColLeftLength;
                colRight.Width = _lastColRightLength;
            }
            else
            {
                _lastColLeftLength = colLeft.Width;
                _lastColRightLength = colRight.Width;

                colLeft.Width = new GridLength(0);
                colRight.Width = new GridLength(150, GridUnitType.Star);
            }
        }

        private void GridSpliteTo_ToRight(object sender, EventArgs e)
        {
            if (colRight.Width.Value == 0)
            {
                return;
            }

            if (colLeft.Width.Value == 0)
            {
                colLeft.Width = _lastColLeftLength;
                colRight.Width = _lastColRightLength;
            }
            else
            {
                _lastColLeftLength = colLeft.Width;
                _lastColRightLength = colRight.Width;

                colRight.Width = new GridLength(0);
                colLeft.Width = new GridLength(300, GridUnitType.Star);
            }
        }

        private string juese = "";
        void Client_GetDengLuJueSeCompleted(object sender, EmpbasicService.GetDengLuJueSeCompletedEventArgs e)
        {
            juese = e.Result;
            if (e.Result != "管理员")
            {
                TreeOfLife.Visibility = System.Windows.Visibility.Collapsed;
                ListBoxOfLife.Visibility = System.Windows.Visibility.Visible;
                Client.GetDengLuOrgAsync(Session.LoginUser.LoginName);
                qihao.Visibility = System.Windows.Visibility.Collapsed;
                biaoti.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                Client.GetCheGaiCountsAsync("", "", "", "0", "未导出", 0, shiti2.djbd);
                Client.GetCheGaiAsync("", "", "", "0", "未导出", 0, 20, 0, shiti2.djbd);
                Client.GetKeyIDsAsync("", "", "0");
                if (TreeOfLife.ItemsSource != null) TreeOfLife.ItemsSource = null;
                TreeOfLife.Visibility = System.Windows.Visibility.Visible;
                ListBoxOfLife.Visibility = System.Windows.Visibility.Collapsed;
                qihao.Visibility = System.Windows.Visibility.Visible;
                biaoti.Visibility = System.Windows.Visibility.Visible;
                Client.GetOrgForHRModelAsync(0);

            }
        }

        void Client_GetDengLuOrgCompleted(object sender, EmpbasicService.GetDengLuOrgCompletedEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.Result))
            {
                Oldorgids = e.Result.Split(',');
            }
            Client.GetOrgForHRModelbyIDsAsync("(" + e.Result + ")");
        }
        void Client_GetOrgForHRModelbyIDsCompleted(object sender, EmpbasicService.GetOrgForHRModelbyIDsCompletedEventArgs e)
        {
            ListBoxOfLife.ItemsSource = e.Result;
            ListBoxOfLife.SelectedIndex = 0;
            orgid = Convert.ToInt32(e.Result[0].ID);
            orgids.Clear();
            orgids.Add(orgid.ToString());
            Client.GetCheGaiCountsAsync(Session.LoginUser.LoginName, "", "", orgids[0], "未导出", 0, shiti2.djbd);
            Client.GetCheGaiAsync(Session.LoginUser.LoginName, "", "", orgids[0], "未导出", 0, 20, 0, shiti2.djbd);
        }

        void Client_GetCheGaiCompleted(object sender, EmpbasicService.GetCheGaiCompletedEventArgs e)
        {
            CheGaiModelList = e.Result;
            if (juese != "管理员")
            {
                DGRole.Columns[1].Visibility = System.Windows.Visibility.Collapsed;
            }
            DGRole.ItemsSource = null;
            DGRole.ItemsSource = e.Result;
        }

        void Client_GetCheGaiCountsCompleted(object sender, EmpbasicService.GetCheGaiCountsCompletedEventArgs e)
        {

            int count = e.Result;
            ObservableCollection<int> source = new ObservableCollection<int>();
            for (int i = 0; i < count; i++)
            {
                source.Add(i + 1);
            }
            PagedCollectionView pcv = new PagedCollectionView(source);
            dataPager1.PageSize = 1;
            dataPager1.Source = pcv;
        }

        private void dataPager1_PageIndexChanged(object sender, EventArgs e)
        {
            int index = dataPager1.PageIndex;
            string ss = "";
            foreach (string ii in orgids)
                ss += ii + ",";
            if (string.IsNullOrEmpty(ss))
            {
                ss = "0";
            }
            else
            {
                ss = ss.Substring(0, ss.Length - 1);
            }
            if (juese != "管理员")
                Client.GetCheGaiAsync(Session.LoginUser.LoginName, datepicker.SelectedDate.ToString(), datepicker1.SelectedDate.ToString(), ss, shiti.ygzt, index * 20, index * 20 + 20, KeyID, shiti2.djbd);
            else
                Client.GetCheGaiAsync("", datepicker.SelectedDate.ToString(), datepicker1.SelectedDate.ToString(), ss, shiti.ygzt, index * 20, index * 20 + 20, KeyID, shiti2.djbd);
        }

        public void Load()
        {
            string ss = "";
            foreach (string ii in orgids)
                ss += ii + ",";
            if (string.IsNullOrEmpty(ss))
            {
                ss = "0";
            }
            else
            {
                ss = ss.Substring(0, ss.Length - 1);
            }
            if (juese != "管理员")
            {
                Client.GetCheGaiCountsAsync(Session.LoginUser.LoginName, datepicker.SelectedDate.ToString(), datepicker1.SelectedDate.ToString(), ss,
                                            shiti.ygzt, KeyID, shiti2.djbd);
                Client.GetCheGaiAsync(Session.LoginUser.LoginName, datepicker.SelectedDate.ToString(), datepicker1.SelectedDate.ToString(), ss, shiti.ygzt, 0,
                                      20, KeyID, shiti2.djbd);
            }
            else
            {
                Client.GetCheGaiCountsAsync("", datepicker.SelectedDate.ToString(), datepicker1.SelectedDate.ToString(), ss,
                                            shiti.ygzt, KeyID, shiti2.djbd);
                Client.GetCheGaiAsync("", datepicker.SelectedDate.ToString(), datepicker1.SelectedDate.ToString(), ss, shiti.ygzt, 0,
                                      20, KeyID, shiti2.djbd);
            }
        }

        private void ChaData_Click(object sender, RoutedEventArgs e)
        {
            Load();
        }

        private void EditData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as EmpbasicService.CheGaiModel;
            if (seljw != null)
            {
                if (seljw.Type == "开卡表")
                {
                    CheGaiBianDongWindow RMWindow2 = new CheGaiBianDongWindow(seljw.ID);
                    RMWindow2.Closed += new EventHandler(RMWindow_Closed3);
                    RMWindow2.Show();
                }
                if (seljw.Type == "变动表")
                {
                    CheGaiWindow RMWindow2 = new CheGaiWindow(seljw.ID);
                    RMWindow2.Closed += new EventHandler(RMWindow_Closed3);
                    RMWindow2.Show();
                }
            }
        }

        private void DeleteData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as CheGaiModel;
            if (seljw != null)
            {
                if (seljw.Status == "已导出")
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("该信息已导出，不可删除！");
                    return;
                }
                if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    Client.DeteleCheGaiAsync(seljw.ID);
                }

            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要删除的车改信息！");
            }
        }

        void Client_DeteleCheGaiCompleted(object sender, DeteleCheGaiCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功！"); ;
            Load();
        }

        void RMWindow_Closed3(object sender, EventArgs e)
        {
            Load();
        }

        private int KeyID = 0;
        void gangwei_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((ComboBox)sender).SelectedItem != null)
            {
                if (((CommunitySystem.PageView.ComObservable)(((ComboBox)sender).SelectedItem)).name != "无")
                    KeyID = Convert.ToInt32(((CommunitySystem.PageView.ComObservable)(((ComboBox)sender).SelectedItem)).name);
                else
                    KeyID = 0;
            }

        }

        void Client_GetKeyIDsCompleted(object sender, GetKeyIDsCompletedEventArgs e)
        {
            tem.Clear();
            tem.Add(new ComObservable { name = "无" });
            foreach (string i in e.Result)
            {
                if (tem.Where(p => p.name == i).Count() == 0) tem.Add(new ComObservable { name = i });
            }
            biaoti.SelectedIndex = 0;

        }

        //void biaoti_DropDownOpened(object sender, EventArgs e)
        //{
        //    string ss = "";
        //    foreach (string ii in orgids)
        //        ss += ii + ",";
        //    if (string.IsNullOrEmpty(ss))
        //    {
        //        ss = "0";
        //    }
        //    else
        //    {
        //        ss = ss.Substring(0, ss.Length - 1);
        //    }
        //    Client.GetKeyIDsAsync(datepicker.SelectedDate.ToString(), datepicker1.SelectedDate.ToString(), ss);
        //}
        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            foreach (EmpbasicService.CheGaiModel p in DGRole.ItemsSource)
            {
                p.IsSelected = (bool)cb.IsChecked;
                if (p.IsSelected)
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(p);
                    }
                }
                else
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() != null)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault());
                    }
                }
            }

        }

        private Stream ss = null;
        private void btnOut_Click(object sender, RoutedEventArgs e)
        {
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (EmpbasicService.CheGaiModel i in (ObservableCollection<EmpbasicService.CheGaiModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (DGRole.ItemsSource != null && selectedempmodel.Count > 0)
            {
                foreach (EmpbasicService.CheGaiModel i in selectedempmodel)
                {
                    if (string.IsNullOrEmpty(i.NewJinE))
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("请先完善数据再进行导出操作！");
                        foreach (EmpbasicService.CheGaiModel ii in (ObservableCollection<EmpbasicService.CheGaiModel>)DGRole.ItemsSource)
                        {
                            ii.IsSelected = false;
                        }
                        selectedempmodel.Clear();
                        return;
                    }
                }
            }
            if (selectedempmodel.Count <= 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要导出的数据！");
                return;
            }
            ObservableCollection<CheGaiModel> tempdengjichegai = new ObservableCollection<CheGaiModel>();
            ObservableCollection<CheGaiModel> tempbiandongchegai = new ObservableCollection<CheGaiModel>();
            foreach (CheGaiModel i in selectedempmodel)
            {
                if (i.Type == "开卡表") tempdengjichegai.Add(i);
                if (i.Type == "变动表") tempbiandongchegai.Add(i);
            }
            if (tempdengjichegai.Count > 0 && tempbiandongchegai.Count > 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("选择的数据只能统一导出一种格式！");
                return;
            }
            var downLoad = new SaveFileDialog()
            {
                Filter = "Excel files(2003) (*.xls)|*.xls",
            };
            if (downLoad.ShowDialog() == true)
            {
                ss = downLoad.OpenFile();
                if (selectedempmodel[0].Type == "变动表")
                    PageInit();
                if (selectedempmodel[0].Type == "开卡表")
                    PageInit1();
            }
        }
        public void PageInit()
        {
            XlsDocument xls = new XlsDocument();
            XF xf = xls.NewXF();
            xf.HorizontalAlignment = HorizontalAlignments.Centered;
            xf.VerticalAlignment = VerticalAlignments.Centered;
            xf.UseBorder = true;
            xf.TopLineStyle = 1;
            xf.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf.BottomLineStyle = 1;
            xf.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf.LeftLineStyle = 1;
            xf.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf.RightLineStyle = 1;
            xf.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf.TextWrapRight = true;
            xf.Font.FontName = "宋体";

            XF xf1 = xls.NewXF();
            xf1.HorizontalAlignment = HorizontalAlignments.Centered;
            xf1.VerticalAlignment = VerticalAlignments.Centered;
            xf1.Font.Bold = true;
            xf1.Font.Height = 16 * 20;
            xf1.Font.FontName = "黑体";

            XF xf3 = xls.NewXF();
            xf3.UseBorder = true;
            xf3.TopLineStyle = 1;
            xf3.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.BottomLineStyle = 1;
            xf3.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.LeftLineStyle = 1;
            xf3.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.RightLineStyle = 1;
            xf3.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.HorizontalAlignment = HorizontalAlignments.Centered;
            xf3.VerticalAlignment = VerticalAlignments.Centered;
            //xf3.Font.Bold = true;
            xf3.Font.Height = 11 * 20;
            xf3.Font.FontName = "宋体";

            XF xf4 = xls.NewXF();
            xf4.HorizontalAlignment = HorizontalAlignments.Centered;
            xf4.VerticalAlignment = VerticalAlignments.Centered;
            //xf4.Font.Bold = true;
            xf4.Font.Height = 11 * 20;
            xf4.Font.FontName = "宋体";
            Worksheet sheet1 = xls.Workbook.Worksheets.AddNamed("徐汇区公务用车制度改革涉改人员公务交通补贴变动审核表");
            sheet1.Cells.Add(1, 1, "期号：" + selectedempmodel[0].KeyID.ToString(), xf4);
            sheet1.Cells.Add(1, 6, selectedempmodel[0].OrgType == "参公系统" ? "行政单位" : "√行政单位", xf4);
            sheet1.Cells.Add(1, 7, selectedempmodel[0].OrgType == "参公系统" ? "√参公事业单位" : "参公事业单位", xf4);
            sheet1.Cells.Merge(2, 2, 1, 8);
            sheet1.Cells.Add(2, 1, "徐汇区公务用车制度改革涉改人员公务交通补贴变动审核表", xf1);
            sheet1.Cells.Merge(3, 3, 1, 8);
            sheet1.Cells.Add(3, 1, "（区委组织部或人力资源和社会保障局审核）", xf4);
            //sheet1.Cells.Merge(4, 4, 1, 3);
            sheet1.Cells.Add(4, 1, "        填表单位（盖章）：", xf4);
            sheet1.Cells.Add(4, 4, "主管部门（盖章）：", xf4);
            sheet1.Cells.Add(4, 7, "审核部门（盖章）：", xf4);

            sheet1.Cells.Merge(5, 6, 1, 1);
            sheet1.Cells.Add(5, 1, "序号", xf3);
            sheet1.Cells.Add(6, 1, "序号", xf3);
            sheet1.Cells.Merge(5, 6, 2, 2);
            sheet1.Cells.Add(5, 2, "姓名", xf3);
            sheet1.Cells.Merge(5, 5, 3, 4);
            sheet1.Cells.Add(5, 3, "变动前", xf3);
            sheet1.Cells.Add(5, 4, "变动前", xf3);
            sheet1.Cells.Add(6, 3, "原职级", xf3);
            sheet1.Cells.Add(6, 4, "原补贴金额（元/月）", xf3);
            sheet1.Cells.Merge(5, 5, 5, 7);
            sheet1.Cells.Add(5, 5, "变动后", xf3);
            sheet1.Cells.Add(5, 6, "变动后", xf3);
            sheet1.Cells.Add(5, 7, "变动后", xf3);
            sheet1.Cells.Add(6, 5, "变动起始年月", xf3);
            sheet1.Cells.Add(6, 6, "现职级", xf3);
            sheet1.Cells.Add(6, 7, "现补贴金额（元/月）", xf3);
            sheet1.Cells.Merge(5, 6, 8, 8);
            sheet1.Cells.Add(5, 8, "变动理由", xf3);
            sheet1.Cells.Add(6, 8, "变动理由", xf3);



            ColumnInfo colInfoxueli1 = new ColumnInfo(xls, sheet1);
            colInfoxueli1.Width = 10 * 256;
            colInfoxueli1.ColumnIndexStart = 0;
            colInfoxueli1.ColumnIndexEnd = 0;
            sheet1.AddColumnInfo(colInfoxueli1);
            ColumnInfo colInfoxueli2 = new ColumnInfo(xls, sheet1);
            colInfoxueli2.Width = 14 * 256;
            colInfoxueli2.ColumnIndexStart = 1;
            colInfoxueli2.ColumnIndexEnd = 1;
            sheet1.AddColumnInfo(colInfoxueli2);
            ColumnInfo colInfoxueli3 = new ColumnInfo(xls, sheet1);
            colInfoxueli3.Width = 20 * 256;
            colInfoxueli3.ColumnIndexStart = 2;
            colInfoxueli3.ColumnIndexEnd = 2;
            sheet1.AddColumnInfo(colInfoxueli3);

            ColumnInfo colInfoxueli4 = new ColumnInfo(xls, sheet1);
            colInfoxueli4.Width = 22 * 256;
            colInfoxueli4.ColumnIndexStart = 3;
            colInfoxueli4.ColumnIndexEnd = 3;
            sheet1.AddColumnInfo(colInfoxueli4);

            ColumnInfo colInfoxueli0 = new ColumnInfo(xls, sheet1);
            colInfoxueli0.Width = 20 * 256;
            colInfoxueli0.ColumnIndexStart = 4;
            colInfoxueli0.ColumnIndexEnd = 4;
            sheet1.AddColumnInfo(colInfoxueli0);

            ColumnInfo colInfoxueli6 = new ColumnInfo(xls, sheet1);
            colInfoxueli6.Width = 16 * 256;
            colInfoxueli6.ColumnIndexStart = 5;
            colInfoxueli6.ColumnIndexEnd = 5;
            sheet1.AddColumnInfo(colInfoxueli6);

            ColumnInfo colInfoxueli7 = new ColumnInfo(xls, sheet1);
            colInfoxueli7.Width = 22 * 256;
            colInfoxueli7.ColumnIndexStart = 6;
            colInfoxueli7.ColumnIndexEnd = 6;
            sheet1.AddColumnInfo(colInfoxueli7);

            ColumnInfo colInfoxueli8 = new ColumnInfo(xls, sheet1);
            colInfoxueli8.Width = 20 * 256;
            colInfoxueli8.ColumnIndexStart = 7;
            colInfoxueli8.ColumnIndexEnd = 7;
            sheet1.AddColumnInfo(colInfoxueli8);
            double oldJinE = 0;
            double newJinE = 0;
            //填充内容    
            if (selectedempmodel.Count > 0)
                for (int i = 0; i < selectedempmodel.Count; i++)
                {
                    sheet1.Cells.Add(i + 7, 1, i + 1, xf);
                    if (selectedempmodel[i].EmpName != null) sheet1.Cells.Add(i + 7, 2, selectedempmodel[i].EmpName.ToString(), xf); else sheet1.Cells.Add(i + 7, 2, "", xf);
                    if (selectedempmodel[i].OldZhiJi != null) sheet1.Cells.Add(i + 7, 3, selectedempmodel[i].OldZhiJi.ToString(), xf);
                    if (selectedempmodel[i].OldJinE != null) sheet1.Cells.Add(i + 7, 4, selectedempmodel[i].OldJinE.ToString(), xf);
                    if (selectedempmodel[i].ChangeDate != null) sheet1.Cells.Add(i + 7, 5, !string.IsNullOrEmpty(selectedempmodel[i].ChangeDate.ToString()) ? Convert.ToDateTime(selectedempmodel[i].ChangeDate).ToString("yyyy-MM-dd") : "", xf); else sheet1.Cells.Add(i + 7, 5, "", xf);
                    if (selectedempmodel[i].NewZhiJi != null) sheet1.Cells.Add(i + 7, 6, selectedempmodel[i].NewZhiJi.ToString(), xf);
                    if (selectedempmodel[i].NewJinE != null) sheet1.Cells.Add(i + 7, 7, selectedempmodel[i].NewJinE.ToString(), xf);
                    if (selectedempmodel[i].Demo != null) sheet1.Cells.Add(i + 7, 8, selectedempmodel[i].Demo.ToString(), xf);
                    oldJinE += Convert.ToDouble(!string.IsNullOrEmpty(selectedempmodel[i].OldJinE) ? selectedempmodel[i].OldJinE : "0");
                    newJinE += Convert.ToDouble(!string.IsNullOrEmpty(selectedempmodel[i].NewJinE) ? selectedempmodel[i].NewJinE : "0");
                }
            sheet1.Cells.Merge(selectedempmodel.Count + 7, selectedempmodel.Count + 7, 1, 3);
            sheet1.Cells.Add(selectedempmodel.Count + 7, 1, "合计：", xf);
            sheet1.Cells.Add(selectedempmodel.Count + 7, 2, "", xf);
            sheet1.Cells.Add(selectedempmodel.Count + 7, 3, "", xf);
            sheet1.Cells.Add(selectedempmodel.Count + 7, 4, oldJinE.ToString(), xf);
            sheet1.Cells.Add(selectedempmodel.Count + 7, 5, "", xf);
            sheet1.Cells.Add(selectedempmodel.Count + 7, 6, "", xf);
            sheet1.Cells.Add(selectedempmodel.Count + 7, 7, newJinE.ToString(), xf);
            sheet1.Cells.Add(selectedempmodel.Count + 7, 8, "", xf);

            sheet1.Cells.Add(selectedempmodel.Count + 8, 1, "     单位主要领导：", xf4);
            sheet1.Cells.Add(selectedempmodel.Count + 8, 4, "填表人：", xf4);
            sheet1.Cells.Add(selectedempmodel.Count + 8, 7, "填报日期：", xf4);
            xls.Save(ss);
            ss.Close();
            ss.Dispose();
            SilverlightMessageBoxLibrary.Message.InfoMessage("导出成功！");
            Client.UpdateCheGaiAllAsync(selectedempmodel);
            Load();
        }

        public void PageInit1()
        {
            XlsDocument xls = new XlsDocument();
            XF xf = xls.NewXF();
            xf.HorizontalAlignment = HorizontalAlignments.Centered;
            xf.VerticalAlignment = VerticalAlignments.Centered;
            xf.UseBorder = true;
            xf.TopLineStyle = 1;
            xf.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf.BottomLineStyle = 1;
            xf.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf.LeftLineStyle = 1;
            xf.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf.RightLineStyle = 1;
            xf.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf.TextWrapRight = true;
            xf.Font.FontName = "宋体";

            XF xf1 = xls.NewXF();
            xf1.HorizontalAlignment = HorizontalAlignments.Centered;
            xf1.VerticalAlignment = VerticalAlignments.Centered;
            xf1.Font.Bold = true;
            xf1.Font.Height = 16 * 20;
            xf1.Font.FontName = "黑体";

            XF xf3 = xls.NewXF();
            xf3.UseBorder = true;
            xf3.TopLineStyle = 1;
            xf3.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.BottomLineStyle = 1;
            xf3.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.LeftLineStyle = 1;
            xf3.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.RightLineStyle = 1;

            xf3.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.HorizontalAlignment = HorizontalAlignments.Centered;
            xf3.VerticalAlignment = VerticalAlignments.Centered;
            //xf3.Font.Bold = true;
            xf3.Font.Height = 11 * 20;
            xf3.Font.FontName = "宋体";

            XF xf4 = xls.NewXF();
            xf4.HorizontalAlignment = HorizontalAlignments.Centered;
            xf4.VerticalAlignment = VerticalAlignments.Centered;
            //xf4.Font.Bold = true;
            xf4.Font.Height = 11 * 20;
            xf4.Font.FontName = "宋体";
            Worksheet sheet1 = xls.Workbook.Worksheets.AddNamed("徐汇区公务用车制度改革涉改单位科级及以下人员公务交通补贴审核表");

            sheet1.Rows.AddRow(1);
            sheet1.Cells.Add(1, 1, "期号：" + selectedempmodel[0].KeyID.ToString(), xf4);
            sheet1.Cells.Add(1, 6, selectedempmodel[0].OrgType == "参公系统" ? "行政单位" : "√行政单位", xf4);
            sheet1.Cells.Add(1, 7, selectedempmodel[0].OrgType == "参公系统" ? "√参公事业单位" : "参公事业单位", xf4);
            sheet1.Cells.Merge(2, 2, 1, 7);
            sheet1.Cells.Add(2, 1, "徐汇区公务用车制度改革涉改单位科级及以下人员公务交通补贴审核表", xf1);
            sheet1.Cells.Merge(3, 3, 1, 7);
            sheet1.Cells.Add(3, 1, "（区委组织部或人力资源和社会保障局审核）", xf4);
            //sheet1.Cells.Merge(4, 4, 1, 3);
            sheet1.Cells.Add(4, 1, "        填表单位（盖章）：", xf4);
            sheet1.Cells.Add(4, 4, "主管部门（盖章）：", xf4);
            sheet1.Cells.Add(4, 6, "审核部门（盖章）：", xf4);

            sheet1.Cells.Add(5, 1, "序号", xf3);
            sheet1.Cells.Add(5, 2, "姓名", xf3);
            sheet1.Cells.Add(5, 3, "性别", xf3);
            sheet1.Cells.Add(5, 4, "现任职务", xf3);
            sheet1.Cells.Add(5, 5, "现任职级", xf3);
            sheet1.Cells.Add(5, 6, "补贴金额（元/月）", xf3);
            sheet1.Cells.Add(5, 7, "备注", xf3);

            ColumnInfo colInfoxueli1 = new ColumnInfo(xls, sheet1);
            colInfoxueli1.Width = 10 * 256;
            colInfoxueli1.ColumnIndexStart = 0;
            colInfoxueli1.ColumnIndexEnd = 0;
            sheet1.AddColumnInfo(colInfoxueli1);
            ColumnInfo colInfoxueli2 = new ColumnInfo(xls, sheet1);
            colInfoxueli2.Width = 14 * 256;
            colInfoxueli2.ColumnIndexStart = 1;
            colInfoxueli2.ColumnIndexEnd = 1;
            sheet1.AddColumnInfo(colInfoxueli2);
            ColumnInfo colInfoxueli3 = new ColumnInfo(xls, sheet1);
            colInfoxueli3.Width = 10 * 256;
            colInfoxueli3.ColumnIndexStart = 2;
            colInfoxueli3.ColumnIndexEnd = 2;
            sheet1.AddColumnInfo(colInfoxueli3);

            ColumnInfo colInfoxueli4 = new ColumnInfo(xls, sheet1);
            colInfoxueli4.Width = 22 * 256;
            colInfoxueli4.ColumnIndexStart = 3;
            colInfoxueli4.ColumnIndexEnd = 3;
            sheet1.AddColumnInfo(colInfoxueli4);

            ColumnInfo colInfoxueli0 = new ColumnInfo(xls, sheet1);
            colInfoxueli0.Width = 20 * 256;
            colInfoxueli0.ColumnIndexStart = 4;
            colInfoxueli0.ColumnIndexEnd = 4;
            sheet1.AddColumnInfo(colInfoxueli0);

            ColumnInfo colInfoxueli6 = new ColumnInfo(xls, sheet1);
            colInfoxueli6.Width = 20 * 256;
            colInfoxueli6.ColumnIndexStart = 5;
            colInfoxueli6.ColumnIndexEnd = 5;
            sheet1.AddColumnInfo(colInfoxueli6);

            ColumnInfo colInfoxueli7 = new ColumnInfo(xls, sheet1);
            colInfoxueli7.Width = 22 * 256;
            colInfoxueli7.ColumnIndexStart = 6;
            colInfoxueli7.ColumnIndexEnd = 6;
            sheet1.AddColumnInfo(colInfoxueli7);
            double oldJinE = 0;
            double newJinE = 0;
            //填充内容    
            if (selectedempmodel.Count > 0)
                for (int i = 0; i < selectedempmodel.Count; i++)
                {
                    sheet1.Cells.Add(i + 6, 1, i + 1, xf);
                    if (selectedempmodel[i].EmpName != null) sheet1.Cells.Add(i + 6, 2, selectedempmodel[i].EmpName.ToString(), xf); else sheet1.Cells.Add(i + 6, 2, "", xf);
                    if (selectedempmodel[i].Gender != null) sheet1.Cells.Add(i + 6, 3, selectedempmodel[i].Gender.ToString(), xf);
                    if (selectedempmodel[i].NowDuty != null) sheet1.Cells.Add(i + 6, 4, selectedempmodel[i].NowDuty.ToString(), xf);
                    if (selectedempmodel[i].NewZhiJi != null) sheet1.Cells.Add(i + 6, 5, selectedempmodel[i].NewZhiJi.ToString(), xf);
                    if (selectedempmodel[i].NewJinE != null) sheet1.Cells.Add(i + 6, 6, selectedempmodel[i].NewJinE.ToString(), xf);
                    if (selectedempmodel[i].Demo != null) sheet1.Cells.Add(i + 6, 7, selectedempmodel[i].Demo.ToString(), xf);
                    oldJinE += Convert.ToDouble(!string.IsNullOrEmpty(selectedempmodel[i].OldJinE) ? selectedempmodel[i].OldJinE : "0");
                    newJinE += Convert.ToDouble(!string.IsNullOrEmpty(selectedempmodel[i].NewJinE) ? selectedempmodel[i].NewJinE : "0");
                }
            sheet1.Cells.Merge(selectedempmodel.Count + 6, selectedempmodel.Count + 6, 1, 5);
            sheet1.Cells.Add(selectedempmodel.Count + 6, 1, "合计：", xf);
            sheet1.Cells.Add(selectedempmodel.Count + 6, 2, "", xf);
            sheet1.Cells.Add(selectedempmodel.Count + 6, 3, "", xf);
            sheet1.Cells.Add(selectedempmodel.Count + 6, 4, "", xf);
            sheet1.Cells.Add(selectedempmodel.Count + 6, 5, "", xf);
            sheet1.Cells.Add(selectedempmodel.Count + 6, 6, newJinE.ToString(), xf);
            sheet1.Cells.Add(selectedempmodel.Count + 6, 7, "", xf);

            sheet1.Cells.Add(selectedempmodel.Count + 7, 1, "     单位主要领导：", xf4);
            sheet1.Cells.Add(selectedempmodel.Count + 7, 4, "填表人：", xf4);
            sheet1.Cells.Add(selectedempmodel.Count + 7, 6, "填报日期：", xf4);
            xls.Save(ss);
            ss.Close();
            ss.Dispose();
            SilverlightMessageBoxLibrary.Message.InfoMessage("导出成功！");
            Client.UpdateCheGaiAllAsync(selectedempmodel);
            Load();
        }
    }
}
